package cn.wendellup.bootStrap.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.wendellup.bootStrap.dao.contract.IUserDao;
import cn.wendellup.bootStrap.valueobject.entity.UserInfo;

@Repository
public class UserDaoImpl implements IUserDao {

	private static Logger logger = Logger.getLogger(UserDaoImpl.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static final String STR_SELECT_USER = " id, firstname, lastname, phone, email ";

	@Override
	public List<UserInfo> listUsers() throws ExceptionCommonBase {
		List<UserInfo> list = new ArrayList<UserInfo>();
		String sql = " select " + STR_SELECT_USER + " from users ";
		logger.info(sql);
		List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : queryResult) {
			UserInfo userInfo = new UserInfo();
			userInfo.setId((Integer) map.get("id"));
			userInfo.setFirstName((String) map.get("firstname"));
			userInfo.setLastName((String) map.get("lastname"));
			userInfo.setPhone((String) map.get("phone"));
			userInfo.setEmail((String) map.get("email"));
			
			list.add(userInfo);
		}

		return list;
	}

}
